package com.cccyy.view;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class PicWithBigAndSmall {
    public static final Logger logger = LoggerFactory.getLogger(PicWithBigAndSmall.class);

    public static void main(String[] args) throws Exception {

        File file = new File("D:\\data\\picture\\PixPin_2024-03-21_16-41-07.png");
        BufferedImage originalImage = ImageIO.read(file);

        // 设置压缩后的最大宽度和高度（单位为像素） 196x46
        int maxWidth = 99;
        int maxHeight = 23;

        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        // 计算压缩比例
        double scaleX = (double) maxWidth / width;
        double scaleY = (double) maxHeight / height;
        double scaleFactor = Math.min(scaleX, scaleY);

        // 创建新的BufferedImage对象，指定压缩后的尺寸
        int newWidth = (int) (width * scaleFactor);
        int newHeight = (int) (height * scaleFactor);


        BufferedImage compImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
        Graphics2D g = compImage.createGraphics();
        g.drawImage(originalImage, 0, 0, newWidth, newHeight, null);
        g.dispose();

        //将压缩后的图片保存到文件
        String newImagePath = "D:\\data\\picture\\img_dpj_t.png";
        ImageIO.write(compImage, "png", new File(newImagePath));


    }
}
